﻿using AutoMapper;
using Hospital.IService;
using Hospital.Model.entity;
using Hospital.Utility.ApiResult;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace Hospital.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class PatientController : ControllerBase
    {
        private readonly IPatientService _iPatientService;
        private readonly IMapper _mapper;
        private ISqlSugarClient _db;

        // 注入服务和 AutoMapper
        public PatientController(IPatientService iPatientService, IMapper mapper, ISqlSugarClient db)
        {
            _iPatientService = iPatientService;
            _mapper = mapper;
            _db = db;
            //_db.DbMaintenance.CreateDatabase();
            //// 手动建多个表
            //_db.CodeFirst.InitTables(
            //    typeof(Patient)
            //);
        }
        // 获取所有患者信息
        [HttpGet]
        public async Task<ActionResult<ApiResult>> GetPatient()
        {
            var patients = await _iPatientService.QueryAsync();
            return ApiResultHelper.Success(patients);
        }
        [HttpDelete("{id}")]
        public async Task<ActionResult<ApiResult>> DeletePatient(int id)
        {
            try
            {
                var result = await _db.Deleteable<Patient>().In(id).ExecuteCommandAsync();
                if (result != 0)
                {
                    return ApiResultHelper.Success(result);
                }
                else
                {
                    return ApiResultHelper.Error("未找到删除条目");
                }
            }
            catch (Exception)
            {
                return ApiResultHelper.Error("删除患者时发生错误");
            }
        }

        // 添加患者
        [HttpPost]
        public async Task<ActionResult<ApiResult>> AddPatient([FromBody] Patient patient)
        {
            try
            {
                // 调用服务层创建患者
                bool isSuccess = await _iPatientService.CreateAsync(patient);

                if (isSuccess)
                {
                    return ApiResultHelper.Success(patient);
                }
                else
                {
                    return ApiResultHelper.Error("添加患者失败");
                }
            }
            catch (Exception)
            {
                return ApiResultHelper.Error("添加时出现错误");
            }

        }

        // 更新患者信息
        [HttpPatch("{id}")]
        public async Task<ActionResult<ApiResult>> PatchPatient(int id, Patient patient)
        {
            try
            {
                if (id == patient.Id)
                {
                    //var result = await _db.Updateable(patient).ExecuteCommandAsync();
                    var result = await _iPatientService.EditAsync(patient);
                    return ApiResultHelper.Success(result);
                }
                else
                {
                    return ApiResultHelper.Error("id:" + id + ",patient.id:" + patient.Id);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
                return ApiResultHelper.Error("更新患者时发生错误");
            }
        }
        [HttpPut("{id}")]
        public async Task<ActionResult<ApiResult>> PutPatient(int id, Patient patient)
        {
            try
            {
                if (id == patient.Id)
                {
                    //var result = await _db.Updateable(patient).ExecuteCommandAsync();
                    var result = await _iPatientService.EditAsync(patient);
                    return ApiResultHelper.Success(result);
                }
                else
                {
                    return ApiResultHelper.Error("id:" + id + ",patient.id:" + patient.Id);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
                return ApiResultHelper.Error("更新患者时发生错误");
            }
        }
    }
}
